<?php

namespace App\Repository;

use Carbon\Carbon;
use EasyWeChat\Foundation\Application;
use Illuminate\Support\Facades\Session;

class wxUserInfoCallBack
{
    //微信中间件返回用户信息
    public function getUserInfo()
    {
        $comfig   = config('wx_2');
        $app      = new Application($comfig);
        $user     = $app->oauth->user();
        $wxOpenid = $user->getId();
        //用户点击评论后查找数据是否已经存在了，如果不存在则在在memberinfo创建数据
        $userInfo = WxRegister::firstOrCreate(
            ['wx_unionid' => $user['original']['unionid']],
            [
                'WeiXID'      => $wxOpenid,
                'MemNumber'   => create_member_number(),
                'NickName'    => $user->getNickname(),
                'YourName'    => $user->getName(),
                'FilePath1'   => $user->getAvatar(),
                'AddDate'     => Carbon::now(),
                'ReleaseDate' => Carbon::now(),
                'wx_unionid'  => $user['original']['unionid']
            ]
        );
        Session::put('wxCallBackUserInfo', $userInfo->toArray());
        return redirect(session('wxURL'));
    }
}